<?php


namespace app\cms\oauth\controller;

use app\community\boot\oauth2\oauthBoot;
use iflow\Container\implement\annotation\tools\data\Inject;
use iflow\Response;
use iflow\Router\Controller;
use iflow\Router\implement\Request\PostMapping;
use League\OAuth2\Server\Grant\RefreshTokenGrant;
use Psr\Http\Message\ResponseInterface;

#[Controller('%oauth2%/authorize')]
class authorizeController
{

    #[Inject]
    public oauthBoot $oauthBoot;

    /**
     * 刷新 AccessToken
     * @return Response|ResponseInterface
     */
    #[PostMapping]
    public function refreshToken(): Response|ResponseInterface
    {
        try {
            return $this->oauthBoot
                -> enableGrantType(
                    new RefreshTokenGrant(
                        $this->oauthBoot -> getRefreshTokenRepository()
                    )
                )
                -> getOauthServer()
                -> respondToAccessTokenRequest(
                    request() -> getServerRequestPsr7(),
                    response() -> getResponsePsr7()
                );
        } catch (\Throwable $exception) {
            return message() -> server_error($exception -> getCode(), $exception -> getMessage());
        }
    }
}